<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible"
    >
      <el-form
        :model="inputForm"
        ref="inputForm"
        v-loading="loading"
        :class="method === 'view' ? 'readonly' : ''"
        :disabled="method === 'view'"
        label-width="120px"
      >
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item
              label="用户手机号"
              prop="user.id"
              :rules="[]"
            >
              <GridSelect
                title="选择用户ID"
                labelName="phone"
                labelValue="id"
                :value="inputForm.user.id"
                :limit="1"
                @getValue="
                  (value) => {
                    inputForm.user.id = value;
                  }
                "
                :columns="[
                  {
                    prop: 'id',
                    label: '主键',
                  },
                  {
                    prop: 'phone',
                    label: '手机号(账号唯一)',
                  },
                  {
                    prop: 'nikeName',
                    label: '昵称',
                  },
                ]"
                :searchs="[
                  {
                    prop: 'phone',
                    label: '手机号(账号唯一)',
                  },
                ]"
                dataListUrl="/userinfo/tUserInfo/list"
                entityBeanName="tUserInfo"
                queryEntityUrl="/userinfo/tUserInfo/queryById"
              >
              </GridSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="所属门店"
              prop="store.id"
            >
              <GridSelect
                title="选择所属门店"
                labelName="storeName"
                labelValue="id"
                :value="inputForm.store.id"
                :limit="1"
                @getValue="
                  (value) => {
                    inputForm.store.id = value;
                  }
                "
                :columns="[
                  {
                    prop: 'storeName',
                    label: '门店名称',
                  },
                  {
                    prop: 'officeAddress',
                    label: '办公地址',
                  },
                  {
                    prop: 'storeContactNumber',
                    label: '门店联系电话',
                  },
                  {
                    prop: 'legalName',
                    label: '联系人',
                  },
                  {
                    prop: 'contactPhoneNumber',
                    label: '联系人电话',
                  },
                ]"
                :searchs="[
                    {
                      prop: 'storeName',
                      label: '门店名称'
                    },
                    {
                      prop: 'storeContactNumber',
                      label: '门店联系电话'
                    }
                    ]"
                dataListUrl="/distributor/vDistributor/list"
                entityBeanName="vDistributor"
                queryEntityUrl="/distributor/vDistributor/queryById"
              >
              </GridSelect>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="门店名称" prop="storeName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.storeName" placeholder="请填写门店名称"     ></el-input>
           </el-form-item>
        </el-col> -->
          <el-col :span="12">
            <el-form-item
              label="真实姓名"
              prop="realName"
              :rules="[]"
            >
              <el-input
                v-model="inputForm.realName"
                placeholder="请填写真实姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="联系方式"
              prop="contactInformation"
              :rules="[]"
            >
              <el-input
                v-model="inputForm.contactInformation"
                placeholder="请填写联系方式"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="身份证正面（信息页）"
              prop="idcardJust"
              :rules="[]"
            >
              <el-upload
                ref="idcardJust"
                v-if="visible"
                list-type="picture-card"
                :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/salesconsultant/vSalesConsultant`"
                :headers="{ token: $cookie.get('token') }"
                :on-preview="
                  (file, fileList) => {
                    showImg(file.url);
                  }
                "
                :on-success="
                  (response, file, fileList) => {
                    inputForm.idcardJust = fileList
                      .map(
                        (item) =>
                          (item.response && item.response.url) || item.url
                      )
                      .join('|');
                  }
                "
                :on-remove="
                  (file, fileList) => {
                    $http
                      .post(
                        `/sys/file/webupload/deleteByUrl?url=${
                          (file.response && file.response.url) || file.url
                        }`
                      )
                      .then(({ data }) => {
                        $message.success(data.msg);
                      });
                    inputForm.idcardJust = fileList
                      .map((item) => item.url)
                      .join('|');
                  }
                "
                :before-remove="
                  (file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`);
                  }
                "
                multiple
                :limit="5"
                :on-exceed="
                  (files, fileList) => {
                    $message.warning(
                      `当前限制选择 5 个文件，本次选择了 ${
                        files.length
                      } 个文件，共选择了 ${
                        files.length + fileList.length
                      } 个文件`
                    );
                  }
                "
                :file-list="idcardJustArra"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="身份证反面（国徽页）"
              prop="idcardBack"
              :rules="[]"
            >
              <el-upload
                ref="idcardBack"
                v-if="visible"
                list-type="picture-card"
                :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/salesconsultant/vSalesConsultant`"
                :headers="{ token: $cookie.get('token') }"
                :on-preview="
                  (file, fileList) => {
                    showImg(file.url);
                  }
                "
                :on-success="
                  (response, file, fileList) => {
                    inputForm.idcardBack = fileList
                      .map(
                        (item) =>
                          (item.response && item.response.url) || item.url
                      )
                      .join('|');
                  }
                "
                :on-remove="
                  (file, fileList) => {
                    $http
                      .post(
                        `/sys/file/webupload/deleteByUrl?url=${
                          (file.response && file.response.url) || file.url
                        }`
                      )
                      .then(({ data }) => {
                        $message.success(data.msg);
                      });
                    inputForm.idcardBack = fileList
                      .map((item) => item.url)
                      .join('|');
                  }
                "
                :before-remove="
                  (file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`);
                  }
                "
                multiple
                :limit="5"
                :on-exceed="
                  (files, fileList) => {
                    $message.warning(
                      `当前限制选择 5 个文件，本次选择了 ${
                        files.length
                      } 个文件，共选择了 ${
                        files.length + fileList.length
                      } 个文件`
                    );
                  }
                "
                :file-list="idcardBackArra"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="审核状态（0：待审核，1：已通过，2：已拒绝，3：待商家审核，4：待平台审核）" prop="auditStatus"
                :rules="[
                 ]">
                <el-select v-model="inputForm.auditStatus" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col> -->
          <el-col :span="12" v-if="inputForm.auditStatus == '2'">
            <el-form-item label="拒绝原因" prop="refuseReason"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.refuseReason" placeholder="请填写拒绝原因"     ></el-input>
           </el-form-item>
        </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="在职状态（0：在职，1：离职）" prop="activeStatus"
                :rules="[
                 ]">
                <el-select v-model="inputForm.activeStatus" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="是否确认离职（0：正常，1：待确认，2：已确认）" prop="confirmResignation"
                :rules="[
                 ]">
                    <el-radio-group v-model="inputForm.confirmResignation">
                        <el-radio v-for="item in $dictUtils.getDictList('')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
                    </el-radio-group>
           </el-form-item>
        </el-col> -->
          <el-col :span="12">
            <el-form-item
              label="备注信息"
              prop="remarks"
              :rules="[]"
            >
              <el-input
                type="textarea"
                v-model="inputForm.remarks"
                placeholder="请填写备注信息"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="visible = false">关闭</el-button>
        <el-button
          type="primary"
          v-if="method != 'view'"
          @click="doSubmit()"
          v-noMoreClick
        >确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import GridSelect from "@/components/gridSelect";
export default {
  data() {
    return {
      title: "",
      method: "",
      visible: false,
      loading: false,
      imageArra: [],
      idcardJustArra: [],
      idcardBackArra: [],
      inputForm: {
        id: "",
        user: {
          id: "",
        },
        store: {
          id: "",
        },
        storeName: "",
        realName: "",
        contactInformation: "",
        idcardJust: "",
        idcardBack: "",
        auditStatus: "",
        refuseReason: "",
        activeStatus: "",
        confirmResignation: "",
        remarks: "",
      },
    };
  },
  components: {
    GridSelect,
  },
  methods: {
    showImg(url) {
      this.$hevueImgPreview(url);
    },
    init(method, id) {
      this.method = method;
      this.inputForm.id = id;
      if (method === "add") {
        this.title = `新建销售顾问(经纪人)信息表`;
      } else if (method === "edit") {
        this.title = "修改销售顾问(经纪人)信息表";
      } else if (method === "view") {
        this.title = "查看销售顾问(经纪人)信息表";
      }
      this.idcardJustArra = [];
      this.idcardBackArra = [];
      this.visible = true;
      this.loading = false;
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields();
        if (method === "edit" || method === "view") {
          // 修改或者查看
          this.loading = true;
          this.$http({
            url: `/salesconsultant/vSalesConsultant/queryById?id=${this.inputForm.id}`,
            method: "get",
          }).then(({ data }) => {
            this.inputForm = this.recover(
              this.inputForm,
              data.vSalesConsultant
            );
            this.inputForm.idcardJust.split("|").forEach((item) => {
              if (item.trim().length > 0) {
                this.idcardJustArra.push({
                  name: decodeURIComponent(
                    item.substring(item.lastIndexOf("/") + 1)
                  ),
                  url: item,
                });
              }
            });
            this.inputForm.idcardBack.split("|").forEach((item) => {
              if (item.trim().length > 0) {
                this.idcardBackArra.push({
                  name: decodeURIComponent(
                    item.substring(item.lastIndexOf("/") + 1)
                  ),
                  url: item,
                });
              }
            });
            this.loading = false;
          });
        }
      });
    },
    // 表单提交
    doSubmit() {
      this.$refs["inputForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http({
            url: `/salesconsultant/vSalesConsultant/save`,
            method: "post",
            data: this.inputForm,
          }).then(({ data }) => {
            this.loading = false;
            if (data && data.success) {
              this.visible = false;
              this.$message.success(data.msg);
              this.$emit("refreshDataList");
            }
          });
        }
      });
    },
  },
};
</script>
